import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '导航栏',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '导航', icon: 'dashboard' }
    }]
  },

]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: '/module',
    component: Layout,
    redirect: '/module/moduleManger',
    name: 'Module',
    meta: { title: '模块', icon: 'user',roles: ['USER']},
    children: [
      {
        path: 'Module',
        name: 'module',
        component: () => import('@/views/module/index'),
        meta: { title: '模块管理', icon: 'user',roles: ['USER']}
      }
    ]
  },

  {
    path: '/person',
    component: Layout,
    redirect: '/person/personInfo',
    name: 'Person',
    meta: { title: '用户信息', icon: 'user',roles: ['USER']},
    children: [
      {
        path: 'personInfo',
        name: 'PersonInfo',
        component: () => import('@/views/person/index'),
        meta: { title: '个人信息', icon: 'user',roles: ['USER']}
      }
    ]
  },

  {
    path: '/article',
    component: Layout,
    redirect: '/article/articleList',
    name: 'Article',
    meta: { title: '文章管理', icon: 'article',roles: ['USER']},
    children: [
      {
        path: 'articleList',
        name: 'ArticleList',
        component: () => import('@/views/article/index'),
        meta: { title: '文章管理', icon: 'article', roles: ['USER']}
      },
      {
        path: 'articleEdit',
        name: 'ArticleEdit',
        component: () => import('@/views/article/articleEdit'),
        meta: { title: '文章编写', icon: 'articleEdit' , roles: ['USER']}
      },
    ]
  },

  {
    path: '/category',
    component: Layout,
    redirect: '/category/categoryList',
    name: 'Category',
    meta: { title: '分类', icon: 'category' ,roles: ['USER']},
    children: [
      {
        path: 'categoryList',
        name: 'CategoryList',
        component: () => import('@/views/category/index'),
        meta: { title: '分类管理', icon: 'category' ,roles: ['USER']}
      }
    ]
  },

  {
    path: '/tags',
    component: Layout,
    redirect: '/tags/tagsList',
    name: 'Tags',
    meta: { title: '标签', icon: 'tags',roles: ['USER']},
    children: [
      {
        path: 'tagsList',
        name: 'TagsList',
        component: () => import('@/views/tags/index'),
        meta: { title: '标签管理', icon: 'tags', roles: ['USER']}
      }
    ]
  },
  {
    path: '/users',
    component: Layout,
    redirect: '/users/usersList',
    name: 'Users',
    meta: { title: '用户', icon: 'users',roles: ['ADMIN']},
    children: [
      {
        path: 'usersList',
        name: 'UsersList',
        component: () => import('@/views/user/index'),
        meta: { title: '用户管理', icon: 'users', roles: ['ADMIN']}
      }
    ]
  },
  {
    path: '/role',
    component: Layout,
    redirect: '/role/roleList',
    name: 'Role',
    meta: { title: '角色', icon: 'role',roles: ['ADMIN']},
    children: [
      {
        path: 'roleList',
        name: 'RoleList',
        component: () => import('@/views/role/index'),
        meta: { title: '角色管理', icon: 'role', roles: ['ADMIN']}
      }
    ]
  },
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/permissionList',
    name: 'Permission',
    meta: { title: '权限', icon: 'permission',roles: ['ADMIN']},
    children: [
      {
        path: 'permissionList',
        name: 'PermissionList',
        component: () => import('@/views/permission/index'),
        meta: { title: '权限管理', icon: 'permission', roles: ['ADMIN']}
      }
    ]
  },
  {
    path: '/search',
    component: Layout,
    redirect: '/search/elasticSearch',
    name: 'Search',
    meta: { title: '搜索引擎', icon: 'search', roles: ['ADMIN']},
    children: [
      {
        path: 'elasticSearch',
        name: 'ElasticSearch',
        component: () => import('@/views/elasticsearch/index'),
        meta: { title: '搜索管理', icon: 'search', roles: ['ADMIN']}
      }
    ]
  },





  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
